import util from '@/libs/util';
import utilDate from '@/util/getDate';
import CONSTANTS from '@/config/constants';
import Cookies from 'js-cookie';
import customTree from '../studentsignup/customTree.vue';
export default {
    name: 'exportable-table',
    components: {
        customTree
    },
    data () {
        return {
            columns: [{
                width:55,
                title: '#',
                type: 'selection'
            }, {
                title: '班级名称',
                key: 'name'
            }, {
                title: '专业',
                key: 'marjorName'
            }, {
                title: '开课时间',
                key: 'classStarttime',
                render: function (h, params) {
                    return h('div', util.getDate(params.row.classStarttime));/* 这里的this.row能够获取当前行的数据 */
                }
            }, {
                title: '预招人数',
                key: 'initiallyPeople',
                render: function (h, params) {
                    return h('div', params.row.initiallyPeople + '(' + params.row.reservedPeople + ')');
                }
            }, {
                title: '负责老师',
                key: 'mainTeacherName'
            }, /* {
                    title: '课程安排',
                    key: 'classSchedules',
                    render:function(h,params){
                        let list = params.rows.classSchedules;

                          /!* return h('div',params.row.initiallyPeople+"("+params.row.reservedPeople+")");*!/
                    }
                }, */ {
                title: '录取方式',
                key: 'classApply'
            }, {
                title: '班级状态',
                key: 'status',
                render: function (h, params) {
                    let text = '';
                    let status = params.row.status;
                    if (status == 1) {
                        text = '草稿';
                    } else if (status == 2) {
                        text = '核审通过';
                    } else if (status == 3) {
                        text = '核审未通过';
                    } else if (status == 4) {
                        text = '等待核审';
                    }
                    return h('div', {}, text);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doApprove(2, obj);
                                }
                            }
                        }, '核审'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doUnApprove(3, obj);
                                }
                            }
                        }, '退回')
                    ]);
                }
            }],
            dataList: [],
            addVisible: false, // 新增弹窗的显示和隐藏
            areaList: [], // 区域列表
            queryData: {}, // 查询表单数据
            pageSize: 5, // 分页数据
            pageIndex: 1, // 分页数据
            total: 0,
            deptModal: false,
            baseuser: JSON.parse(Cookies.get('baseUser')),
            query: {},
            departmentList: [],
            selection: []// 选中的行
        };
    },
    mounted () {
        this.init();
    },
    methods: {
        init: function () { // 页面初始化
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'AREA_TYPE'}
            )).then(res => { // 获得区域数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.areaList = data;
                }
            });

            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'CLASS_SEASON'}
            )).then(res => { // 学期数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.classSeasonList = data;
                }
            });

            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'MAJOR_LEVEL'}
            )).then(res => { // 获得区域数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.majorLevelList = data;
                }
            });

            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'CLASS_AGE'}
            )).then(res => { // 年纪数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.classAgeList = data;
                }
            });

            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'CLASS_APPLY_TYPE'}
            )).then(res => { // 获得区域数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.classApplyList = data;
                }
            });

            // util.ajax.post('/base/organize/list', util.toFormData(
            //     {parentid:0}
            // )).then(res => {//获得部门
            //   console.log(res);
            //     if(res.data.code==200){
            //         var data  =res.data.data.list;
            //         this.departmentList=data;
            //     }
            // });

            util.ajax.post('/busine/major/findByParent', util.toFormData(
                {parentid: 0}
            )).then(res => { // 获得专业大类
                console.log(res);
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.marjorFatherList = data;
                }
            });
            this.doQuery();
        },
        doQuery: function () {
            util.ajax.post('/base/class/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.name ? this.query.name : '',
                year: this.query.year ? this.query.year : '',
                classSeason: this.query.classSeason ? this.query.classSeason : '',
                areaId: this.query.areaId ? this.query.areaId : '',
                departmentId: this.query.departmentId ? this.query.departmentId : '',
                marjorId: this.query.marjorId ? this.query.marjorId : '',
                status: '4',
                organizeId: this.baseuser.orgId
            })).then(res => {
                if (res.data.data) {
                    this.dataList = res.data.data.list;
                    for (let i in this.dataList) {
                        this.dataList[i].classStarttime = utilDate.getDate(this.dataList[i].classStarttime, 'date');
                    }
                    this.total = res.data.data.total;
                }
            });
        },
        setDept () {
            if (this.$refs.dept.selectData[0]) {
                this.query.departmentId = this.$refs.dept.selectData[0].id;
                this.query.departmentName = this.$refs.dept.selectData[0].title;
            } else {
                this.query.departmentId = '';
                this.query.departmentName = '';
            }
            this.deptModal = false;
        },
        cancelSelect () {
            this.$refs.dept.cancelSelect();
        },
        refresh () {
            this.query = {};
            this.loadList();
        },
        doApprove: function (flag, ids) {
            this.approve(flag, ids);
        },
        doUnApprove: function (flag, ids) {
            this.approve(flag, ids);
        },
        doApproveBath: function () {
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择核审项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.approve(2, ids);
        },
        doUnApproveBath: function () {
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择退回项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.approve(3, ids);
        },
        approve: function (flag, ids) { // flag 0是核审1是退回
            util.ajax.post('/base/class/doApprove', util.toFormData({
                ids: ids.join('@'),
                flag: flag
            })).then(res => {
                if (res.data.code == 200) {
                    if (res.data.message) {
                        this.$Message.success('操作成功!');
                    } else {
                        this.$Message.error('操作失败!');
                    }
                } else {
                    this.$Message.error('操作失败!');
                }
                this.init();
            });
        },
        whenSelectRow: function (selection) {
            this.selection = selection;
        }
    }
};
